<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="app">
        <button @click="flag='CompA'">组件A</button>
        <button @click="flag='CompB'">组件B</button>
        <button @click="flag='CompC'">组件C</button>
        <hr>

        <!-- 缓存非活动的组件 -->
        <keep-alive>
            <component :is="flag"></component>
        </keep-alive>
    </div>
</body>
<script>
    new Vue({
        el:'#app',
        data:{
            flag:'CompA'
        },
        components:{
            CompA:{
                template:'<h2>CompA</h2>',
                created() {
                    console.log('CompA created()')
                },
                destroyed() {
                    console.log('CompA destroyed()')
                },
            },
            CompB:{
                template:'<h2>CompB</h2>',
                created() {
                    console.log('CompB created()')
                },
                destroyed() {
                    console.log('CompB destroyed()')
                },
            },
            CompC:{
                template:'<h2>CompC</h2>',
                created() {
                    console.log('CompC created()')
                },
                destroyed() {
                    console.log('CompC destroyed()')
                },
            }
        }
    })
</script>
</html>